package com;

/**
 * @Author:xuyuanhong
 * @Date:Created in 2022/12/15 9:46
 */

import org.json.JSONArray;
import org.json.JSONObject;

import javax.lang.model.SourceVersion;
import javax.tools.Tool;
import java.io.*;
import java.util.Random;
import java.util.Set;

public class Text {
    public static void main(String[] args) throws IOException, InterruptedException {
//        while(true){


        Random random= new Random();//创建random实例对象，程序中会用，用于产生随机数
//        String [] allID =  {{1,},{2},};

        Tool tool= new Tool() {
            @Override
            public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) {
                return 0;
            }

            @Override
            public Set<SourceVersion> getSourceVersions() {
                return null;
            }
        };//创建格式化json字符串工具类
        JSONArray jsonArray=new JSONArray();//创建JSONArray对象
        File file=new File("./in/student.json");
        if(!file.exists())//判断文件是否存在，若不存在则新建
        {
            file.createNewFile();
        }
        FileOutputStream fileOutputStream=new FileOutputStream(file);//实例化FileOutputStream
        OutputStreamWriter outputStreamWriter=new OutputStreamWriter(fileOutputStream,"utf-8");//将字符流转换为字节流
        BufferedWriter bufferedWriter= new BufferedWriter(outputStreamWriter);//创建字符缓冲输出流对象

        for(int i=0;i<5;i++){
            StringBuffer valSb = new StringBuffer();
            StringBuffer valSb1 = new StringBuffer();

            String charStr = "abcdefghijklmnopqrstuvwxyz";
            String charStr1 = "LA";
            int charLength = charStr.length();
            int charLength1 = charStr.length();


            for (int j = 0; i < 5; j++) {

                int index = random.nextInt(charLength);

                valSb.append(charStr.charAt(index));

            }
            for (int j = 0; i < 2; j++) {

                int index = random.nextInt(charLength1);

                valSb1.append(charStr1.charAt(index));

            }



            JSONObject jsonObject=new JSONObject();//创建JSONObject对象
            jsonObject.put("ID",random.nextInt(3)+205815);//产生205815-205817的随机数
            jsonObject.put("name", valSb.toString());//产生18-25的随机数
            jsonObject.put("Sex",random.nextInt(1)+0);
            jsonObject.put("Date",random.nextInt(20)+20020901);
            jsonObject.put("Num",random.nextInt(20)+1000000002);
            jsonObject.put("Status", valSb1.toString());
            jsonObject.put("Terms",random.nextInt(1)+9);
            jsonArray.put(jsonObject);//将jsonObject对象旁如jsonarray数组中
        }
        String jsonString=jsonArray.toString();//将jsonarray数组转化为字符串
//        String JsonString=tool.stringToJSON(jsonString);//将jsonarrray字符串格式化
        bufferedWriter.write(jsonString);//将格式化的jsonarray字符串写入文件
//        bufferedWriter.flush();//清空缓冲区，强制输出数据
        bufferedWriter.close();//关闭输出流
            Thread.sleep(1000000);
//    }
}
}
